#include "shapewidget.h"
#include <QDebug>
#include <QPainter>
#include <QBitmap>

ShapeWidget::ShapeWidget(const QString &background, const int width, const int height,
                           QWidget *parent) : QWidget(parent), m_pixmap_background(nullptr)
{
    m_pixmap_background = new QPixmap();
    if (!m_pixmap_background->load(background)) {
        qDebug() << "background pixmap load fail";
    }
    if (width == 0 || height == 0) {
        this->resize(m_pixmap_background->size());
    }
    else {
        m_pixmap_background->scaled(width, height);
    }
    this->setMask(QBitmap(m_pixmap_background->mask()));
}

ShapeWidget::~ShapeWidget()
{
    if (m_pixmap_background) delete m_pixmap_background;
}

void ShapeWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.drawPixmap(0, 0, *m_pixmap_background);
}

